export default function ({
	url,
	method,
	data,
	header
}) {
	let BASE_URL = getApp().globalData.BASE_URL
	// #ifdef H5
	BASE_URL = ''
	// #endif
	const token = uni.getStorageSync('token') || '';
	const headers = header || {}
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_URL}${url}`,
			dataType: 'json',
			method: method || 'GET',
			header: {
				"Content-Type": "application/json;charset=UTF-8",
				"Accept": "*/*",
				"Authorization": token || '',
				...headers,
			},
			data: data || {},
			success: (res) => {
				const showToast = uni.$util.showToast
				const result = res.data
				const pages = getCurrentPages()
				const currPage = pages[pages.length - 1]; //当前页面
				const fullPath = currPage.$page.fullPath
				// console.log('success----', res.data);
				if (header && header.resolve) return resolve(result)
				if (result.code == 'A0307' || result.code == 'B0301') {
					uni.setStorageSync('token', '')
					if (fullPath == '/pages/login/login') return
					return uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				if (result.code != '00000') {
					showToast(result.message)
					return reject(result)
				}
				resolve(result)
			},
			fail: (err) => {
				console.log('fail----', res.data);
				reject(err)
			}
		});
	})
}